/*
 * Copyright (c) Jiandong.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { ViewManagerResolver } from './ViewManagerResolver';
import { View } from './View';
import { ReactShadowNode } from './ReactShadowNode';
import { ViewManager } from './ViewManager';
import { ComponentCallbacks2 } from './ComponentCallbacks2';
import HashMap from '@ohos.util.HashMap'

export type ReactShadowNodeViewManager = ViewManager<View, ReactShadowNode>

export class ViewManagerRegistry extends ComponentCallbacks2 {
    private mViewManagers: HashMap<string, ReactShadowNodeViewManager>
    private mViewManagerResolver?: ViewManagerResolver;

    public constructor(viewManagerResolver: ViewManagerResolver) {
        super();
        this.mViewManagers = new HashMap<string, ReactShadowNodeViewManager>()
        this.mViewManagerResolver = viewManagerResolver;
    }

//    public ViewManagerRegistry(List<ViewManager> viewManagerList) {
//        HashMap<string, ViewManager> viewManagerMap = MapBuilder.newHashMap();
//        Iterator var3 = viewManagerList.iterator();
//
//        while(var3.hasNext()) {
//            ViewManager viewManager = (ViewManager)var3.next();
//            viewManagerMap.put(viewManager.getName(), viewManager);
//        }
//
//        this.mViewManagers = viewManagerMap;
//        this.mViewManagerResolver = null;
//    }
//
//    public ViewManagerRegistry(Map<String, ViewManager> viewManagerMap) {
//        this.mViewManagers = (Map)(viewManagerMap != null ? viewManagerMap : MapBuilder.newHashMap());
//        this.mViewManagerResolver = null;
//    }

    public get(className: string): ReactShadowNodeViewManager {
        let viewManager = this.mViewManagers.get(className) as ReactShadowNodeViewManager
        if (viewManager != null) {
            return viewManager;
        } else if (this.mViewManagerResolver != null) {
            viewManager = this.getViewManagerFromResolver(className);
            if (viewManager != null) {
                return viewManager;
            } else {
                throw new Error("ViewManagerResolver returned null for " + className + ", existing names are: " + this.mViewManagerResolver.getViewManagerNames());
            }
        } else {
            throw new Error("No ViewManager found for class " + className);
        }
    }

    private getViewManagerFromResolver(className: string): ViewManager<View, ReactShadowNode> {
        let viewManager = this.mViewManagerResolver.getViewManager(className) as ReactShadowNodeViewManager
        if (viewManager != null) {
            this.mViewManagers.set(className, viewManager);
        }

        return viewManager;
    }

    getViewManagerIfExists(className: string): ReactShadowNodeViewManager {
        let viewManager = this.mViewManagers.get(className) as ReactShadowNodeViewManager
        if (viewManager != null) {
            return viewManager;
        } else {
            return this.mViewManagerResolver != null ? this.getViewManagerFromResolver(className) : null;
        }
    }
//
//    public void onSurfaceStopped(final int surfaceId) {
//        Runnable runnable = new Runnable() {
//            public void run() {
//                Iterator var1 = ViewManagerRegistry.this.mViewManagers.entrySet().iterator();
//
//                while(var1.hasNext()) {
//                    Entry<String, ViewManager> entry = (Entry)var1.next();
//                    ((ViewManager)entry.getValue()).onSurfaceStopped(surfaceId);
//                }
//
//            }
//        };
//        if (UiThreadUtil.isOnUiThread()) {
//            runnable.run();
//        } else {
//            UiThreadUtil.runOnUiThread(runnable);
//        }
//
//    }
//
//    public onTrimMemory(level: number) {
//        Runnable runnable = new Runnable() {
//            public void run() {
//                Iterator var1 = ViewManagerRegistry.this.mViewManagers.entrySet().iterator();
//
//                while(var1.hasNext()) {
//                    Entry<String, ViewManager> entry = (Entry)var1.next();
//                    ((ViewManager)entry.getValue()).trimMemory();
//                }
//
//            }
//        };
//        if (UiThreadUtil.isOnUiThread()) {
//            runnable.run();
//        } else {
//            UiThreadUtil.runOnUiThread(runnable);
//        }
//
//    }
//
//    public void onConfigurationChanged(Configuration newConfig) {
//    }
//
//    public void onLowMemory() {
//        this.onTrimMemory(0);
//    }
}
